package com.example.controller;

import com.example.pojo.Poetry;
import com.example.service.PoetryService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class SearchController {
    @Resource
    private PoetryService poetryService;
    public static int city_code=0,city_inner_num=0,code_start=0,before_page=0;
    @PostMapping ("/search/{key}/{flag}")//搜索
    public List<HashMap<String,Object>> search_key(@PathVariable String key,@PathVariable int flag,@RequestBody Map<String,Object> req) {

        List ListOfHash =new ArrayList();
        int start=0,end=0,page=0,perPage=0;
        if((Number)req.get("page")!=null&&(Number)req.get("perPage")!=null){//分页展示
            page=((Number)req.get("page")).intValue();
            perPage=((Number)req.get("perPage")).intValue();
            start=page*perPage;
            end=(page+1)*perPage-1;
        }

        if(flag==1){//地区搜索
            int num=0;
             for(int i=0;i<poetryService.search_area_name(key).size();i++){
                 if(!poetryService.search_area_name(key).get(i).getCode().equals("")&&poetryService.get_locate_poetry(poetryService.search_area_name(key).get(i).getCode())!=0){
                     if(num!=0){
                         start=0;
                         end=perPage-num-1;
                     }
                     if(poetryService.search_area_name(key).get(i).getCode().endsWith("0000")){
                         int locate_id=Integer.parseInt(poetryService.search_area_name(key).get(i).getCode().substring(0,2));
                         for(int j=start;j<=end;j++){
                             HashMap<String,Object> hashmap=new HashMap<>();
                             if(j<poetryService.locate_all_poetry(locate_id).size()){
                                 num++;
                                 hashmap.put("id",poetryService.locate_all_poetry(locate_id).get(j).getId()); //id作为主键 后期都以id来检索数据库！
                                 hashmap.put("name",poetryService.locate_all_poetry(locate_id).get(j).getPoetry_name());
                                 //System.out.println("省内全部市："+poetryService.locate_all_poetry(locate_id).get(i).getPoetry_name());
                                 hashmap.put("image",poetryService.locate_all_poetry(locate_id).get(j).getImage());
                                 String summary=poetryService.locate_all_poetry(locate_id).get(j).getSummary();
                                 int index=summary.indexOf("►");
                                 hashmap.put("summary",summary.substring(0,index));//去除三角形后的部分
                                 ListOfHash.add(hashmap);
                             }
                             else{
                                 break;
                             }
                         }
                     }
                     else{
                         int locate_id=Integer.parseInt(poetryService.search_area_name(key).get(i).getCode());
                         for(int j=start;j<=end;j++){
                             HashMap<String,Object> hashmap=new HashMap<>();
                             if(j<poetryService.locate_and_poetry(locate_id).size()){
                                 num++;
                                 hashmap.put("id",poetryService.locate_and_poetry(locate_id).get(j).getId()); //id作为主键 后期都以id来检索数据库！
                                 hashmap.put("name",poetryService.locate_and_poetry(locate_id).get(j).getPoetry_name());
                                 hashmap.put("image",poetryService.locate_and_poetry(locate_id).get(j).getImage());
                                 String summary=poetryService.locate_and_poetry(locate_id).get(j).getSummary();
                                 int index=summary.indexOf("►");
                                 hashmap.put("summary",summary.substring(0,index));//去除三角形后的部分
                                 ListOfHash.add(hashmap);
                             }
                             else{
                                 break;
                             }
                         }
                     }
                 }

            }
        }

//            before_page=page;
//            int z=1,code=0,total=0;
//
//            int num=0,flag_pro=1,pro_code=0;
//            if(city_code!=0){
//                code=city_code;
//            }
//            else{//重新开始找
//                if (poetryService.search_area_name(key).get(code_start).getCode().endsWith("0000")) {//关键词为省
//                    flag_pro = 0;
//                    pro_code = Integer.parseInt(poetryService.search_area_name(key).get(code_start).getCode().substring(0, 2));//取走前两位
//                    code = Integer.parseInt(poetryService.province_below(pro_code).get(z));
//                    System.out.println("first:"+code);
//                    //省下的市遍历第z个城市 编码
//                }
//                else {//定位至普通的市
//                    code = Integer.parseInt(poetryService.search_area_name(key).get(code_start).getCode());
//                }
//
////                int len=poetryService.search_area_name(key).size();
////                for(int j=code_start;j<len;j++) {
////                    if (poetryService.search_area_name(key).get(j).getCode().endsWith("0000")) {//关键词为省
////                        flag_pro = 0;
////                        pro_code = Integer.parseInt(poetryService.search_area_name(key).get(j).getCode().substring(0, 2));//取走前两位
////                        if (num != 0) {//非初始
////                            while (total <= (page + 1) * perPage) {//20
////                                total += poetryService.get_locate_poetry(poetryService.province_below(pro_code).get(z));//+10+20
////                                z = (z + 1) % (poetryService.province_below(pro_code).size());//z=1+1+1
////                            }
////                            z--;//下标为z的还没遍历完  Total是加上该下标，遍历完的总个数
////                            if (z < 0) z = poetryService.province_below(pro_code).size() - 1;
////                        }
////                        code = Integer.parseInt(poetryService.province_below(pro_code).get(z));
////                        System.out.println("first:"+code);
////                        //省下的市遍历第z个城市 编码
////
////                    }
////                    else {//定位至普通的市
////                        code = Integer.parseInt(poetryService.search_area_name(key).get(j).getCode());
////                    }
////                }
//
//            }
//
//            start=city_inner_num+(page-before_page-1)*perPage;
//            end=start+(perPage-num-1);
//            city_code=code;
//
////                if(num<perPage&&num!=0){//前一个j完成
////                    start=0;//下一个市 从头起始
////                    end=perPage-num-1;
////                }
////                else{
////                    if(z!=1){
////                        start=page*perPage-(total-poetryService.locate_and_poetry(Integer.parseInt(poetryService.province_below(pro_code).get(z))).size());
////                        end=start+perPage-1;
////                    }
////                    else{
////                        start=page*perPage;
////                        end=(page+1)*perPage-1;
////                    }
////                }
//            if(start<poetryService.get_locate_poetry(String.valueOf(code))){
//                for(int i=start;i<=end;i++){
//                    System.out.println(code+" "+start+" "+i+" "+num);
//                    int len_city=poetryService.get_locate_poetry(String.valueOf(code));//某个市的诗人数量
//                    System.out.println(len_city);
//                    if(i<len_city){//模糊匹配 sql用like concat
//                        HashMap<String,Object> hashmap=new HashMap<>();
//                        System.out.println(poetryService.locate_and_poetry(code).get(i).getPoetry_name());
//                        hashmap.put("id",poetryService.locate_and_poetry(code).get(i).getId());
//                        hashmap.put("name",poetryService.locate_and_poetry(code).get(i).getPoetry_name());
//                        hashmap.put("image",poetryService.locate_and_poetry(code).get(i).getImage());
//                        String summary=poetryService.locate_and_poetry(code).get(i).getSummary();
//                        int index=summary.indexOf("►");
//                        hashmap.put("summary",summary.substring(0,index));//去除三角形后的部分
//                        ListOfHash.add(hashmap);
//                        num++;
//                        if(num==perPage){
//                            city_inner_num=i+1;//为下一页做准备 继续找原来的code 从inner_num开始
//                            break;
//                        }
//                    }
//                    else{//该市没弄完
//                        code_start++;
//                        city_code=0;
//                    }
//                }
//            }
//            else{
//                while ()
//            }
//            if(num!=perPage){//需要去下一个市
//
//                if(flag_pro==0){
//                    if(num!=0){
//                        z=(z+1)%(poetryService.province_below(pro_code).size());
//                    }
//                    code=Integer.parseInt(poetryService.province_below(pro_code).get(z));//该省其他市继续循环
////                    if(num<perPage&&num!=0){//前一个j完成
////                        start=0;//下一个市 从头起始
////                        end=perPage-num-1;
////                        i=start-1;
////                    }
////                    else{
////                        break;
////                    }
//                    System.out.println(num);
//                    System.out.println(start+" "+end);
//                }
//                else {//本身就是市 证明无了 去下一个j
//                    //break;
//                }
//            }
//            if(flag_pro==0){
//                flag_pro=1;
//            }
////            if(num==perPage){
////                break;
////            }
//        }
//
//
//
//


        else{//诗人人名搜索
            start=page*perPage;
            end=(page+1)*perPage-1;
            for(int i=start;i<=end;i++){
                if(i<poetryService.search_poetry_name(key).size()){//模糊匹配 sql用like concat
                    HashMap<String,Object> hashmap=new HashMap<>();
                    hashmap.put("id",poetryService.search_poetry_name(key).get(i).getId());
                    hashmap.put("name",poetryService.search_poetry_name(key).get(i).getPoetry_name());
                    hashmap.put("image",poetryService.search_poetry_name(key).get(i).getImage());
                    String summary=poetryService.search_poetry_name(key).get(i).getSummary();
                    int index=summary.indexOf("►");
                    hashmap.put("summary",summary.substring(0,index));//去除三角形后的部分
                    ListOfHash.add(hashmap);
                }
                else{
                    break;
                }
            }
        }
        return ListOfHash;
    }

    @GetMapping("/search/{key}/{flag}/{perPage}")//搜索界面分页数量
    public HashMap<String,Object> locate_pagenum(@PathVariable String key,@PathVariable int flag,@PathVariable int perPage) {
        HashMap<String,Object> hashmap=new HashMap<>();
        int total=0;
        if(flag==1){//地区
            for(int i=0;i<poetryService.search_area_name(key).size();i++){
                if(!poetryService.search_area_name(key).get(i).getCode().equals("")&&poetryService.get_locate_poetry(poetryService.search_area_name(key).get(i).getCode())!=0){
                    total+=poetryService.get_locate_poetry(poetryService.search_area_name(key).get(i).getCode());
                }
            }
        }
        else{
            total=poetryService.search_poetry_name(key).size();
        }
        int page_num=total/perPage;
        if(poetryService.count_poetry()%perPage!=0){
            page_num++;
        }
        hashmap.put("page_num",page_num);
        return hashmap;
    }
}
